package com.example.zkweixiu.app.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zkweixiu.app.R;
import com.example.zkweixiu.app.bean.LikeUserBean;
import com.example.zkweixiu.app.bean.ObjBean;
import com.example.zkweixiu.app.pro.PublicPro;
import com.google.gson.Gson;
import com.yanzhenjie.sofia.Sofia;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

// 我的  修改个性签名

public class SignActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_text;
    private TextView tv_names;
    private String trim;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign);
        Sofia.with(SignActivity.this).statusBarDarkFont().statusBarBackground(Color.parseColor("#FFFFFF"));
        initView();

    }

    private void updata() {
        OkHttpUtils
                .get()
                .url(PublicPro.SIGN_UP)
                .addParams("sign", et_text.getText().toString().trim())
                .addParams("wx_uuid", PublicPro.userBean.getData().getGuaUuid())
                .addParams("token", PublicPro.userBean.getData().getGuaToken())
                .build()
                .execute(new StringCallback() {


                    @Override
                    public void onError(okhttp3.Call call, Exception e, int id) {
                        Log.e("HomeFragment1", e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("HomeFragment1", response);

                        ObjBean objBean = new Gson().fromJson(response, ObjBean.class);

                        if ( objBean.getStatus() == 200) {

                            Toast.makeText(SignActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                            finish();
                            return;
                        } else {

                            Toast.makeText(SignActivity.this, objBean.getMsg(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_fanhui:
                finish();
                break;
            case R.id.iv_fanhui2:
                finish();
                break;

            case R.id.bt_confirm:
                String trim = et_text.getText().toString().trim();

                if (TextUtils.isEmpty(trim)){
                    Toast.makeText(this, "签名不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                updata();

                break;
        }
    }


    private void initView() {
        ImageView iv_fanhui = findViewById(R.id.iv_fanhui);
        View iv_fanhui2 = findViewById(R.id.iv_fanhui2);

        et_text = findViewById(R.id.et_text);
        tv_names = findViewById(R.id.tv_names);
        Button bt_confirm = findViewById(R.id.bt_confirm);

        iv_fanhui.setOnClickListener(this);
        iv_fanhui2.setOnClickListener(this);
        bt_confirm.setOnClickListener(this);


        et_text.setText(PublicPro.userBean.getData().getGuaSigna());

        String cdui = et_text.getText().toString().trim();
        tv_names.setText(cdui.length() + "/16");
        et_text.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                //    Log.e("tv_data6", "beforeTextChanged");
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                //    Log.e("tv_data6", "onTextChanged");

            }

            @Override
            public void afterTextChanged(Editable editable) {
                //  Log.e("tv_data6", "afterTextChanged");
                // 接口调用 模糊查询
                //  tv_data5.setText(end);
                String name = editable.toString().trim();
                int editEnd = et_text.getSelectionEnd();
                int editStart = et_text.getSelectionStart();
                if (name.length() >= 17) {
                    editable.delete(editStart-1, editEnd);
          /*          et_text.removeTextChangedListener(this);
                    et_text.setText(name.substring(0, 16));
                    et_text.addTextChangedListener(this);*/


                } else {
                    tv_names.setText(name.length() + "/16");

                }


            }
        });
    }

}
